package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysRegisterDTO;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserMapper userMapper;

    @Override
    public Integer queryByMobile(String mobile) {
        LambdaQueryWrapper<SysUser > wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getMobile, mobile);

        Integer count = userMapper.selectCount(wrapper);
        return count;
    }

    @Autowired
    SmsService smsService;

    /**
     * 1. 健壮性判断
     * 2. 校验验证码
     * 3. 用户信息存储到数据库中
     * @param sysRegisterDTO
     * @return
     */
    @Override
    public Long register(SysRegisterDTO sysRegisterDTO) {
        if(sysRegisterDTO == null || sysRegisterDTO.getMobile() == null || sysRegisterDTO.getCheckcode() == null || sysRegisterDTO.getPassword() == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        Boolean flag = smsService.verify(sysRegisterDTO.getMobile(), SMSConstant.SMS_TYPE_REGISTER, sysRegisterDTO.getCheckcode());
        if( ! flag){
            throw new NcException(ResponseEnum.CODE_IMAGE_ERROR);
        }


        SysUser user = new SysUser();
        user.setMobile(sysRegisterDTO.getMobile());
        //对密码进行加密存储
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user.setPassword(passwordEncoder.encode(sysRegisterDTO.getPassword()));

        user.setCreateTime(new Date());
        user.setStatus((short)1);

        //用户信息存储到数据库
        userMapper.insert(user);


        return user.getId();
    }

    @Override
    public SysUserDTO queryUser(String username) {
        if(username == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username).or().eq(SysUser::getMobile, username);

        SysUser user = userMapper.selectOne(wrapper);
        if(user == null){
            throw new NcException(ResponseEnum.USER_NOT_FOUND);
        }


        return BeanHelper.copyProperties(user, SysUserDTO.class);
    }
}
